﻿<?php
require_once('../../../config.php');
 require_once($CFG->libdir . '/accesslib.php');
 require_once($CFG->libdir . '/adminlib.php');
 require_once($CFG->dirroot . '/local/myplugin/lib.php');
require_once($CFG->dirroot . "/local/myplugin/forms.php");
define('AJAX_SCRIPT', true);

//Yêu cầu người dùng đăng nhập và kiểm tra xem người dùng có quyền sử dụng trang này ko
require_login($SITE);
require_capability('local/myplugin:managecurriculum', context_system::instance());

global $DB;
$path = $CFG->wwwroot.'/local/myplugin/curriculum/add_subject.php';

/* Ket noi den file javascript - module.js */
$cssdir =  $CFG->wwwroot.'/local/myplugin/css/mycss.css';
echo '<link rel="stylesheet" href="'.$cssdir.'" type="text/css" media="screen" />';
echo '<script src="module.js"></script>';

//==================start colorbox=================
echo '<script src="../colorbox/jquery.min.js"></script>';
echo '<link rel="stylesheet" href="../css/colorbox.css" />';
echo '<script src="../colorbox/jquery.colorbox.js"></script>';
//browser detection
echo '<script language="javascript" defer="false">
//browser detection
    var agt=navigator.userAgent.toLowerCase();
    var is_major = parseInt(navigator.appVersion);
    var is_minor = parseFloat(navigator.appVersion);

    var is_nav  = ((agt.indexOf("mozilla")!=-1) && (agt.indexOf("spoofer")==-1)
                && (agt.indexOf("compatible") == -1) && (agt.indexOf("opera")==-1)
                && (agt.indexOf("webtv")==-1) && (agt.indexOf("hotjava")==-1));
    var is_nav4 = (is_nav && (is_major == 4));
    var is_nav6 = (is_nav && (is_major == 5));
    var is_nav6up = (is_nav && (is_major >= 5));
    var is_ie     = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
</script>';

$program_id = optional_param('program',null, PARAM_TEXT);

$option=array();

/* Chuan bi trang web can hien thi */	
$url = new moodle_url($path);
$PAGE->set_url($url);
$PAGE->set_title('Subject');
$PAGE->set_heading('Subject');
$PAGE->set_pagelayout('standard');

//Them thanh menu ngang ung voi trang trong duong link
navigation_node::override_active_url(new moodle_url($path, array('param'=>'value')));

echo $OUTPUT->header();

/* Viet doan code cua ban o day */
//add tab vào trang
$mylib = new curriculum_management();

$add_subject_form = new form_add_subject(null, array('program'=>$program_id));
$add_subject_form->display();

//Chỉ cho nhập số dương ở ô số tín chỉ, số giờ lý thuyết, số giờ thực hành
echo '<script>allow_positive_numeric_input_only("id_txttinchi");</script>';
echo '<script>allow_positive_numeric_input_only("id_txtlythuyet");</script>';
echo '<script>allow_positive_numeric_input_only("id_txtthuchanh");</script>';

//bat su kien nhan nu cancel
echo $q;
if ($add_subject_form->is_cancelled()) {

echo 'hủy thao tác';

}

//kiem tra neu dc submitted thì làm gì
if ($fromform = $add_subject_form->get_data()) 
{
	$record = array();
	$record['subject_id'] = $fromform->txtid;
	$record['subject_name_en'] = $fromform->txtteneng;
	$record['subject_name_vn'] = $fromform->txttenvn;
	$record['subject_credit'] = $fromform->txttinchi;
	$record['subject_theory_hr'] = $fromform->txtlythuyet;
	$record['subject_practice_hr'] = $fromform->txtthuchanh;
	$record['abstract'] = $fromform->txtdesc;
	$record['program_id'] = $fromform->cbprogram;
    echo "<script>clear_form_elements('#mform1');</script>";
    
    $id_subject = NULL;
    $id_subject = $mylib->add_subject($record);
    
	if($id_subject != NULL) {
		echo '<script type="text/javascript">success_mess("'.constant("ADD_SUBJECT_SUCCESSFUL").'");clear_form_elements("#mform1");</script>';
		echo '<script>location.href="'.$path.'?program='.$fromform->cbprogram.'"</script>';
	}
	else {
		echo "<script  type='text/javascript'>success_mess('Môn học chưa được thêm!');</script>";
	}
    
    if($fromform->cbprogram != 'all')
    {
    $ta = $fromform->ta_temp;
   
    $ins = $fromform->instructor_temp;
  
    
    if($ins != NULL)
    {
    // Cắt chuỗi
    $ins_id = explode(',,', $ins);
    $n = count($ins_id);
    $role = 1;
    //add_teacher
    $mylib->add_teacher_subject($n,$id_subject,$ins_id,$role);
    }
    else
    {
        $role = 1;
        $teachers_array = array();
        $teachers_array = $DB->get_records('teacher_subject', array('subject'=>$id_subject,'role'=>$role));

        foreach($teachers_array as $teacher_array)
        {
        $DB->delete_records('teacher_subject', array('subject'=>$id_subject,'role'=>$role));
        }
    }
    
    if($ta != NULL)
    {
    
    $ta_id = explode(',,', $ta);
    
    $n2 = count($ta_id);
   
    $role= 2;
    $mylib->add_teacher_subject($n2,$id_subject,$ta_id,$role);
    }
    else
    {
        $role = 2;
        $teachers_array = array();
        $teachers_array = $DB->get_records('teacher_subject', array('subject'=>$id_subject,'role'=>$role));

        foreach($teachers_array as $teacher_array)
        {
        $DB->delete_records('teacher_subject', array('subject'=>$id_subject,'role'=>$role));
        }
    }
    }
  }

/*------Ket thuc doan code cua ban-------*/

echo $OUTPUT->footer();

